package com.thiswhyme.demo.msgpush.core.impl.nio;

import com.thiswhyme.common.expection.BusinessException;
import com.thiswhyme.demo.msgpush.core.WsConstant;
import com.thiswhyme.demo.msgpush.core.config.MsgPushProperties;
import com.thiswhyme.demo.msgpush.core.impl.nio.event.BroadcastEventNioHandler;
import com.thiswhyme.demo.msgpush.core.impl.nio.event.CloseEventNioHandler;
import com.thiswhyme.demo.msgpush.core.impl.nio.event.ConnectEventNioHandler;
import com.thiswhyme.demo.msgpush.core.impl.nio.event.PushEventNioHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;

/**
 * @description: NIO实现配置
 * @author: thiswhyme
 * @date: 2021/9/30
 */
@ConditionalOnProperty(value = "msg-push.websocket.impl", havingValue = WsConstant.IMPL_NIO)
@Configuration
public class NioConfiguration {

    @Bean
    public NioWsHandler nioWsHandler() {
        return new NioWsHandler();
    }

    @Bean
    public NettyServer nettyServer(NioWsHandler nioWsHandler, MsgPushProperties msgPushProperties) {
        MsgPushProperties.NioProperties nioProperties = Optional.ofNullable(msgPushProperties.getNioProperties())
                .orElseThrow(() -> new BusinessException("NIO配置不完整"));
        return new NettyServer(nioWsHandler, nioProperties);
    }

    @Bean
    public BroadcastEventNioHandler broadcastEventNioHandler() {
        return new BroadcastEventNioHandler();
    }

    @Bean
    public CloseEventNioHandler closeEventNioHandler() {
        return new CloseEventNioHandler();
    }

    @Bean
    public ConnectEventNioHandler connectEventNioHandler() {
        return new ConnectEventNioHandler();
    }

    @Bean
    public PushEventNioHandler pushEventNioHandler() {
        return new PushEventNioHandler();
    }
}
